/**
 * DSM - Distributed Search Manager
 * Developed by Milspec Research International Pty Ltd
 * $Author: gsingh $
 * $Revision: 1.2 $
 * $Date: 2006/04/18 07:04:48 $
 * (c)Copyright 2004
 * education.au limited
 * DEST
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 * Neither the names education.au limited, DEST nor the names of its
 * contributors may be used to endorse or promote products derived from this
 * software without specific prior written permission.
 * To the extent permitted by law, the copyright owners of this software and
 * its contributors:
 * (i) exclude all warranties in relation to the software; and
 * (ii) exclude liability for all losses, costs, expenses and damages arising
 * in any way from the use of the software whether arising from or in
 * relation to breach of contract, negligence or any other tort, in equity
 * or otherwise. If the software is in breach of a warranty which is
 * implied by law, the copyright owners and contributors liability is
 * limited to the replacement of the software.
 *
 * @author	gsingh
 */

package au.edu.educationau.opensource.dsm.obj;

import au.edu.educationau.opensource.dsm.util.Flog;
import au.edu.educationau.opensource.dsm.util.EducationAuUtils;

/**
 * Turns a URL into a Z39.50 stored object for DSM to understand. The URL will
 * contain a servername which becomes the target, port (self) and URI (which is
 * the database name.
 */

public class Z3950URL extends java.lang.Object implements java.io.Serializable {
	private String target = "";
	private int port = 210;
	private String database = "";
	private String schema = SMEvents.Z3950_BIB_1; // BIB-1 is the default
	private String buffer = "";

	public Z3950URL(String url) {
		buffer = url;
		try {
			String _schema = url.substring(0, url.indexOf(':')).toUpperCase();
			String _target = url.substring(url.indexOf(':') + 3, url.lastIndexOf(':'));
			String _port = url.substring(url.lastIndexOf(':') + 1, url.lastIndexOf('/'));
			String _database = url.substring(url.lastIndexOf('/') + 1);
			target = _target.trim();
			port = EducationAuUtils.getInteger(_port, 100, 99999);
			database = _database.trim();

			if (_schema.equals("BIB1")) {
				schema = SMEvents.Z3950_BIB_1;
			} else if (_schema.equals("GRS1")) {
				schema = SMEvents.Z3950_GRS_1;
			} else if (_schema.equals("MARC21")) {
				schema = SMEvents.Z3950_MARC_21;
			}

		} catch (Exception oe) {
			Flog.error("o.m.d.o.Z3950URL", "Badly formed Z39.50 format URL", oe);
		}
	}

	/**
	 * Get the target of the Z39.50 server
	 */
	public String getTarget() {
		return this.target;
	}

	/**
	 * Get the port of the Z39.50 server
	 */
	public int getPort() {
		return this.port;
	}

	/**
	 * Get the database name of the Z39.50 server
	 */
	public String getDatabase() {
		return this.database;
	}

	/**
	 * Get the schema to use with the server target
	 */
	public String getSchema() {
		return this.schema;
	}

	/**
	 * Returns the URL in Z39.50 form
	 */
	public String toString() {
		return buffer;
	}

	/**
	 * Checks if the domain name, port and database match
	 * 
	 * @param o
	 *            the Z3950URL to compare to
	 */
	public boolean equals(Object o) {
		if (null != o) {
			return (((Z3950URL) o).getTarget().equals(this.getTarget()) && ((Z3950URL) o).getPort() == this.getPort() && ((Z3950URL) o).getDatabase().equals(this.getDatabase()) && ((Z3950URL) o).getSchema().equals(this.getSchema()));
		} else {
			return false;
		}
	}
}

// -- EOF

